import 'package:flower_flutter/ExercisePage.dart';
import 'package:flower_flutter/SliverWidgetPage.dart';
import 'package:flower_flutter/provider/provider_state.dart';
import 'package:flower_flutter/time_picker.dart';
import 'package:flower_flutter/widget/provider_state_page_2.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:toast/toast.dart';

import 'BasicWidget.dart';
import 'DialogWidget.dart';
import 'DragglePage.dart';
import 'Exercise2Page.dart';
import 'Exercise3Page.dart';
import 'Exercise4Page.dart';
import 'ExpansionPanelListPage.dart';
import 'ExpansionTilePage.dart';
import 'FlowLayoutPage.dart';
import 'GridViewWidgetPageState.dart';
import 'ListViewWidgetPage.dart';
import 'MultiChildWidgetPage.dart';
import 'PopupMenuButtonPage.dart';
import 'SingleChildWidgetPage.dart';
import 'TabBarWidgetPage.dart';
import 'TextFieldWidgetPage.dart';
import 'ValueNotifierPage.dart';
import 'WrapWidgetPage.dart';
import 'chip_page.dart';
import 'constraint_page.dart';
import 'dart_keywords.dart';
import 'global_widget_page.dart';

void main() {
  // runApp(MyApp());
  runApp(MultiProvider(
      providers: [ChangeNotifierProvider(create: (_) => ProviderState())], child: MyApp()));
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  void initState() {
    super.initState();
    Future f1 = new Future(() => null);
    Future f2 = new Future(() => null);
    // Future f3 = new Future(() => {print("创建f3")});
    // f3.then((_) => print("我是f3"));
//then 异步操作在then里面执行
    f2.then((_) {
      print("我是f2");
      new Future(() => print("我是一个新的"));
      // f1.then((_) {
      //   print("我是f1");
      // });
    }).then((value) => print("我还是f2"));
    Future.delayed(Duration(milliseconds: 2000), () {
      return 'cc';
    }).then((value) => print(value));
  }

  @override
  Widget build(BuildContext context) {
    DateTime _lastPressedTime;
    return SafeArea(
      child: Scaffold(
        body: SingleChildScrollView(
          child: Container(
            alignment: Alignment.center,
            padding: EdgeInsets.all(25),
            child: Column(
              mainAxisSize: MainAxisSize.max,
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                RaisedButton(
                    child: Text("基础组件"),
                    textColor: Colors.red,
                    onPressed: () => {push(BasicWidgetPage())}),
                RaisedButton(
                    child: Text("表单组件"),
                    textColor: Colors.red,
                    onPressed: () => {push(TextFieldWidgetPage())}),
                RaisedButton(
                    child: Text("单子布局"),
                    textColor: Colors.red,
                    onPressed: () => {push(SingleChildWidgetPage())}),
                RaisedButton(
                    child: Text("多子布局(Stack)"),
                    textColor: Colors.red,
                    onPressed: () => {push(MultiChildWidgetPage())}),
                RaisedButton(
                    child: Text("ListView"),
                    textColor: Colors.red,
                    onPressed: () => {push(ListViewWidgetPage())}),
                Divider(),
                RaisedButton(
                    child: Text("GridView"),
                    textColor: Colors.red,
                    onPressed: () => {push(GridViewWidgetPageState())}),
                RaisedButton(
                    child: Text("Sliver"),
                    textColor: Colors.red,
                    onPressed: () => {push(SliverWidgetPage())}),
                RaisedButton(
                    child: Text("TabBar"),
                    textColor: Colors.red,
                    onPressed: () => {push(TabBarWidgetPage())}),
                ElevatedButton(
                    child: Text(
                      "流式布局组件(wrap)",
                      style: TextStyle(color: Colors.white),
                    ),
                    style: ButtonStyle(backgroundColor: MaterialStateProperty.all(Colors.blue)),
                    onPressed: () => {push(WrapWidgetPage())}),
                RaisedButton(
                    child: Text("布局小练习1"),
                    textColor: Colors.red,
                    onPressed: () => {push(ExercisePage())}),
                RaisedButton(
                    child: Text("布局小练习2"),
                    textColor: Colors.red,
                    onPressed: () => {push(Exercise2Page())}),
                RaisedButton(
                    child: Text("布局小练习3（画板）"),
                    textColor: Colors.red,
                    onPressed: () => {push(Exercise3Page())}),
                RaisedButton(
                    child: Text("布局小练习4（banner）"),
                    textColor: Colors.red,
                    onPressed: () => {push(Exercise4Page())}),
                RaisedButton(
                    child: Text("PopupMenuButton"),
                    textColor: Colors.red,
                    onPressed: () => {push(PopupMenuButtonPage())}),
                RaisedButton(
                    child: Text("拖拽widget"),
                    textColor: Colors.red,
                    onPressed: () => {push(DraggablePage())}),
                RaisedButton(
                    child: Text("时间选择"),
                    textColor: Colors.red,
                    onPressed: () => {push(TimePicker())}),
                RaisedButton(
                    child: Text("ExpansionTile"),
                    textColor: Colors.red,
                    onPressed: () => {push(ExpansionTilePage())}),
                RaisedButton(
                    child: Text("ExpansionPanelList"),
                    textColor: Colors.red,
                    onPressed: () => {push(ExpansionPanelListPage())}),
                ElevatedButton(
                  child: Text('ValueNotifier'),
                  style: ButtonStyle(backgroundColor: MaterialStateProperty.all(Colors.blue)),
                  onPressed: () => push(ValueNotifierPage()),
                ),
                ElevatedButton(
                  child: Text('Provider'),
                  style: ButtonStyle(backgroundColor: MaterialStateProperty.all(Colors.blue)),
                  onPressed: () => push(ProviderPage_2()),
                ),
                ElevatedButton(
                  child: Text('GlobalKey'),
                  style: ButtonStyle(backgroundColor: MaterialStateProperty.all(Colors.blue)),
                  onPressed: () => push(GlobalKeyWidget()),
                ),
                ElevatedButton(
                  child: Text('弹框'),
                  style: ButtonStyle(backgroundColor: MaterialStateProperty.all(Colors.blue)),
                  onPressed: () => push(DialogWidget()),
                ),
                ElevatedButton(
                  child: Text('布局约束'),
                  style: ButtonStyle(backgroundColor: MaterialStateProperty.all(Colors.blue)),
                  onPressed: () => push(ConstraintPage()),
                ),
                ElevatedButton(
                  child: Text('流式布局单选'),
                  style: ButtonStyle(backgroundColor: MaterialStateProperty.all(Colors.blue)),
                  onPressed: () => push(FlowLayoutPage()),
                ),
                ElevatedButton(
                  child: Text('chip'),
                  style: ButtonStyle(backgroundColor: MaterialStateProperty.all(Colors.pink)),
                  onPressed: () => push(ChipPage()),
                ),
                ElevatedButton(
                  child: Text('dart关键字理解（extends，with，implements ）'),
                  style: ButtonStyle(backgroundColor: MaterialStateProperty.all(Colors.pink)),
                  onPressed: () => push(DartKeyWordsPage()),
                ),
                WillPopScope(
                  child: SizedBox(),
                  onWillPop: () async {
                    if (_lastPressedTime == null ||
                        DateTime.now().difference(_lastPressedTime) > Duration(seconds: 2)) {
                      //两次点击间隔超过1秒则重新计时
                      _lastPressedTime = DateTime.now();
                      Toast.show('再次点击退出', context);
                      return false;
                    }
                    return true;
                  },
                )
              ],
            ),
          ),
        ),
      ),
    );
  }

  void push(Widget widget) {
    Navigator.push(context, MaterialPageRoute(builder: (context) => widget));
    //Navigator.of(context).popUntil((route) => false);//删除所有路由
  }
}
